package com.draven.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.draven.mall.entity.DispatchConfig;
import com.draven.mall.mapper.DispatchConfigMapper;
import com.draven.mall.service.DispatchConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 配送调度配置服务实现类
 */
@Service
public class DispatchConfigServiceImpl extends ServiceImpl<DispatchConfigMapper, DispatchConfig> implements DispatchConfigService {

    @Override
    public DispatchConfig getActiveConfig() {
        LambdaQueryWrapper<DispatchConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DispatchConfig::getIsActive, 1);
        return getOne(queryWrapper);
    }

    @Override
    public List<DispatchConfig> getAllConfigs() {
        LambdaQueryWrapper<DispatchConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(DispatchConfig::getUpdatedTime);
        return list(queryWrapper);
    }

    @Override
    @Transactional
    public boolean activateConfig(Long configId) {
        // 先将所有配置设为非激活状态
        LambdaUpdateWrapper<DispatchConfig> deactivateWrapper = new LambdaUpdateWrapper<>();
        deactivateWrapper.set(DispatchConfig::getIsActive, 0)
                        .eq(DispatchConfig::getIsActive, 1);
        update(deactivateWrapper);
        
        // 再将指定配置设为激活状态
        LambdaUpdateWrapper<DispatchConfig> activateWrapper = new LambdaUpdateWrapper<>();
        activateWrapper.eq(DispatchConfig::getId, configId)
                      .set(DispatchConfig::getIsActive, 1)
                      .set(DispatchConfig::getUpdatedTime, new Date());
        return update(activateWrapper);
    }

    @Override
    public boolean addConfig(DispatchConfig config) {
        // 如果是新配置且设置为激活状态，则将其他配置都设为非激活
        if (config.getIsActive() != null && config.getIsActive() == 1) {
            LambdaUpdateWrapper<DispatchConfig> deactivateWrapper = new LambdaUpdateWrapper<>();
            deactivateWrapper.set(DispatchConfig::getIsActive, 0)
                            .eq(DispatchConfig::getIsActive, 1);
            update(deactivateWrapper);
        }
        
        return save(config);
    }

    @Override
    @Transactional
    public boolean updateConfig(DispatchConfig config) {
        // 如果更新的配置设置为激活状态，则将其他配置都设为非激活
        if (config.getIsActive() != null && config.getIsActive() == 1) {
            LambdaUpdateWrapper<DispatchConfig> deactivateWrapper = new LambdaUpdateWrapper<>();
            deactivateWrapper.set(DispatchConfig::getIsActive, 0)
                            .eq(DispatchConfig::getIsActive, 1)
                            .ne(DispatchConfig::getId, config.getId());
            update(deactivateWrapper);
        }
        
        return updateById(config);
    }

    @Override
    public boolean deleteConfig(Long configId) {
        // 检查是否为激活状态的配置
        DispatchConfig config = getById(configId);
        if (config != null && config.getIsActive() != null && config.getIsActive() == 1) {
            // 不允许删除激活状态的配置
            return false;
        }
        
        return removeById(configId);
    }
} 